package com.zx.dsptuneup_TriMic.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.base.tools.tools.StringUtils;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddDeviceAdapter2 extends BaseAdapter {
    private Context context;
    private List<DeviceBean> gridViewBeanList;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    public List<DeviceBean> getGridViewBeanList() {
        return gridViewBeanList;
    }
    private DetailViewHolderListener mListener; // 用于回调记录填入的信息

    public void setGridViewBeanList(List<DeviceBean> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        mSelectedPositions = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    public AddDeviceAdapter2(Context context, List<DeviceBean> gridViewBeanList) {
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
//        this.mListener = mListener;
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    /**
     * 全选
     * */
    public void sellectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,true);
        }
        notifyDataSetChanged();
    }

    /**
     * 取消全选
     * */
    public void cancelSelectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,false);
        }
        notifyDataSetChanged();
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }
    //获得选中条目的结果
    public ArrayList<DeviceBean> getSelectedItem() {
        ArrayList<DeviceBean> selectList = new ArrayList<>();
        for (int i = 0; i < gridViewBeanList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(gridViewBeanList.get(i));
            }
        }
        return selectList;
    }


    @Override
    public int getCount() {
        return gridViewBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return gridViewBeanList.get(i);
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final ViewHolder viewHolder;
        DeviceBean deviceBean = gridViewBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_list_add_device2, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final View itemView = view;
        viewHolder.mac_txt.setText(StringUtils.isEmpty(deviceBean.getMac()) ? "未知mac地址" : deviceBean.getMac());
        viewHolder.ip_txt.setText(StringUtils.isEmpty(deviceBean.getIp()) ? "未知ip地址" : deviceBean.getIp());

        //清除焦点
        viewHolder.name_txt.clearFocus();
        viewHolder.adress_txt.clearFocus();
        //先清除以前的文本改变监听
        if (viewHolder.name_txt.getTag() instanceof TextWatcher) {
            viewHolder.name_txt.removeTextChangedListener((TextWatcher) (viewHolder.name_txt.getTag()));
        }
        if (viewHolder.adress_txt.getTag() instanceof TextWatcher) {
            viewHolder.adress_txt.removeTextChangedListener((TextWatcher) (viewHolder.adress_txt.getTag()));
        }

        // status 1 正常 2 忙碌 3 异常 4 掉线
//        GradientDrawable gd = (GradientDrawable)viewHolder.ststus_circle.getBackground();
//        switch (deviceBean.getStatus()){
//            case 1:
//                viewHolder.equip_status.setText("已上线");
//                gd.setColor(Color.parseColor("#5BCB00"));
//                break;
//            case 2:
//                viewHolder.equip_status.setText("忙碌");
//                gd.setColor(Color.parseColor("#486FFF"));
//                break;
//            case 3:
//                viewHolder.equip_status.setText("异常");
//                gd.setColor(Color.parseColor("#FEAA00"));
//                break;
//            case 4:
//                viewHolder.equip_status.setText("掉线");
//                gd.setColor(Color.parseColor("#D2D2D2"));
//                break;
//        }
        //文本改变监听
        final TextWatcher oneNameWatcher = new SimpeTextWather() {
            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    deviceBean.setDeviceName(null);
                } else {
                    deviceBean.setDeviceName(String.valueOf(s));
                }
            }
        };
        final TextWatcher oneAgeWatcher = new SimpeTextWather() {
            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    deviceBean.setPositionName(null);
                } else {
                    deviceBean.setPositionName(String.valueOf(s));
                }
            }
        };

        //吧监听设置到不一样的EditText上
        viewHolder.name_txt.addTextChangedListener(oneNameWatcher);
        viewHolder.name_txt.setTag(oneNameWatcher);

        viewHolder.adress_txt.addTextChangedListener(oneAgeWatcher);
        viewHolder.adress_txt.setTag(oneAgeWatcher);

//        if(mListener != null){
//            mListener.setData(viewHolder, i);
//        }

        viewHolder.sel_check.setSelected(isItemChecked(i));

        //条目view的监听
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemChecked(i)) {
                    setItemChecked(i, false);
                } else {
                    setItemChecked(i, true);
                }
                notifyDataSetChanged();
            }
        });
        return view;
    }

    public abstract class SimpeTextWather implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    }

    /**
     * 展现不一样数据的接口
     */
    public interface DetailViewHolderListener {
        void setData(ViewHolder viewHolder, int position);
    }

    static public class ViewHolder {
        @BindView(R.id.mac_txt)
        TextView mac_txt;
        @BindView(R.id.ip_txt)
        TextView ip_txt;
        @BindView(R.id.sel_check)
        TextView sel_check;
        @BindView(R.id.name_txt)
        public EditText name_txt;
        @BindView(R.id.adress_txt)
        public EditText adress_txt;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}